package com.yykj.drup.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yykj.drup.server.dto.PrescriptionProductDto;
import com.yykj.drup.server.dto.PrescriptionStoreProductDto;
import com.yykj.drup.server.mapper.OrderDetailsDao;
import com.yykj.drup.server.model.OrderDetails;
import com.yykj.drup.server.model.Product;
import com.yykj.drup.server.service.OrderDetailsService;
import com.yykj.drup.server.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zengg
 * @since 2022-03-22
 */
@Service
@AllArgsConstructor
public class OrderDetailsServiceImp extends ServiceImpl<OrderDetailsDao, OrderDetails> implements OrderDetailsService {

    final ProductService productService;


    @Override
    public Boolean insertOrderDetails(Integer orderId, List<OrderDetails> orderDetails) {
        List<OrderDetails> orderDetailList = new ArrayList<>();
        boolean batch = false;
        if (!orderDetails.isEmpty()){
            orderDetails.stream().forEach(orderDetail ->{
                orderDetail.setOrderId(orderId);
                orderDetailList.add(orderDetail);
            });
            batch = this.saveBatch(orderDetailList);
        }
        return batch;
    }

    @Override
    public List<OrderDetails> orderDetailsInit(PrescriptionStoreProductDto prescriptionStoreProductDto) {

        List<OrderDetails> orderDetailsList = new ArrayList<>();
        List<PrescriptionProductDto> products = prescriptionStoreProductDto.getProducts();
        products.stream().forEach(product -> {
                Product selectProduct = productService.getById(product.getId());
                orderDetailsList.add(
                        OrderDetails
                                .builder()
                                .stroeId(prescriptionStoreProductDto.getId())
                                .stroeName(prescriptionStoreProductDto.getShopName())
                                .productId(product.getId())
                                .productPrice(selectProduct.getPrice())
                                .productName(selectProduct.getTitle())
                                .productImg(selectProduct.getProductImg())
                                .productSkuAlias(selectProduct.getSkuName())
                                .productNumber(product.getProductNumber())
                                .productApprovalNumber(selectProduct.getApprovalNumber())
                                .productManufacturer(selectProduct.getManufacturer())
                                .build());
            });
        return orderDetailsList;
    }

    @Override
    public List<OrderDetails> findOrderDetailsByOrderId(Integer orderId) {

        QueryWrapper<OrderDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        return this.list(wrapper);
    }
}
